package com.flamingo.xiaomiweather.ui.place;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flamingo.xiaomiweather.R;
import com.flamingo.xiaomiweather.logic.model.PlaceResponse;

public class PlaceFragment extends Fragment {

    private PlaceViewModel viewModel;

    private PlaceAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_place,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(linearLayoutManager);
        TextView searchPlaceEdit = view.findViewById(R.id.searchPlaceEdit);
        ImageView bgImageView = view.findViewById(R.id.bgImageView);
        searchPlaceEdit.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String content = editable.toString();
                if (!content.isEmpty()) {
                    viewModel.searchPlaces(content);
                } else {
                    recyclerView.setVisibility(View.GONE);
                    bgImageView.setVisibility(View.VISIBLE);
                    viewModel.setPlaceResponse(new PlaceResponse());
                    adapter.notifyDataSetChanged();
                }
            }
        });

        // 观察 placeLiveData 的变化
        viewModel.placeLiveData.observe(getViewLifecycleOwner(), result -> {
            PlaceResponse places = result.getData();
            if (places != null) {
                recyclerView.setVisibility(View.VISIBLE);
                bgImageView.setVisibility(View.GONE);
                viewModel.setPlaceResponse(places);
                adapter = new PlaceAdapter(this,viewModel.getPlaceResponse());
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            } else {
                Toast.makeText(getActivity(), "未能查询到任何地点", Toast.LENGTH_SHORT).show();
                if (result.getError() != null) {
                    result.getError().printStackTrace();
                }
            }
        });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 懒加载 ViewModel
        viewModel = new ViewModelProvider(this).get(PlaceViewModel.class);
    }
}
